package cn.inther.print.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import lombok.SneakyThrows;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import net.sourceforge.barbecue.BarcodeException;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Kevin
 * @date 2023/2/20 16:50
 * @description
 **/
@RestController
public class JasperReportsPrinter {

  @SneakyThrows
  @RequestMapping
  public String printer(){

    return "打印完成";

  }

  public static void main(String[] args) {
    printerPdf();
  }

  @SneakyThrows
  public static void printerPdf(){
    // 需要编译的模板路径
    String jrxmlPath = "D:\\Projetcs\\Xiaohong-Project\\JavaPrint\\src\\main\\resources\\demo.jrxml";
    // 编译完成后的路径
    String jasperlPath = "D:\\Projetcs\\Xiaohong-Project\\JavaPrint\\src\\main\\resources\\demo.jasper";

    // 编译模板，编译为jasper后缀的二进制文件
    JasperCompileManager.compileReportToFile(jrxmlPath,jasperlPath);

    // 构造数据
    List<Map> list = new ArrayList<>();
    Map map = new HashMap();
    map.put("brand","鸟牌");
    map.put("boxNo","111222333444");
    map.put("category","外星人");
    map.put("totalNum","100");
    map.put("location","中国大陆");
    map.put("scanCode","111111111");
    list.add(map);

    // 填充数据
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperlPath, null, new JRBeanCollectionDataSource(list));

    // 输出文件
    String pdfPath = "D:\\Projetcs\\Xiaohong-Project\\JavaPrint\\src\\main\\resources\\demo.pdf";

    JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);

//    File pdfFile = new File(pdfPath);
//    String printerName = "Inther Prienter";//打印机名包含字串
//
//    RawPrinter.PDFprint(pdfFile,printerName);

  }



}


